﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoGo3
{


    
 
    class Game
    {

        private const int GameWidth = 550;
        private const int GameHeight = 400;
        private List<Brick> Bricks { get; set; }
        /// <summary>
        /// 游戏自带的Timer
        /// </summary>
        public System.Timers.Timer Timer { get; set; }

        //定义委托
        public delegate void GameChanged();
        //定义时间
        public event GameChanged gameChanged;

        private Point[] _brickPoints =
            {
            new Point(10,10),
            new Point(110,10),
            new Point(210,10),
            new Point(310,10),
            new Point(400,10),
            new Point(480,10),
            new Point(10,80),
            new Point(110,80),
            new Point(210,80),
            new Point(310,80),
            new Point(400,80),
            new Point(480,80),
        };

        private Bar _bar;
        private Ball _ball;

        public Game()
        {
            _bar = new Bar(GameWidth);
            _ball = new Ball(new Rectangle(255,350 -30,30,30));
            //初始化Bricks
            Bricks = new List<Brick>();
            foreach (var brickPoint in _brickPoints)
            {
                Brick brick = new Brick(new Rectangle(brickPoint, new Size(55, 15)));
                Bricks.Add(brick);
            }
            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
            
           

        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟处理程序
            _ball.Move(new Size(GameWidth,GameHeight));
            //处理碰撞
            HandleTouch();
            //通知form游戏改变了
            gameChanged();
            
           

            
        }
        /// <summary>
        /// 处理输赢
        /// </summary>

        public bool GameOver()
        {
            if (this._ball.Rectangle.Y + this._ball.Rectangle.Height > GameHeight)
            {
                return true;
            }
            return false;
        }
        public bool Victory()
        {
           return Bricks.Count == 0;
        }

        /// <summary>
        /// 处理碰撞
        /// </summary>

        private void HandleTouch()
        {
            if(_ball.Rectangle.IntersectsWith(_bar.Rectangle))
            {
                //小球和bar撞上了
                _ball.ChangeVerDiction();
            }

            Brick brickNow = null;
            foreach(var brick in Bricks )
            {
                if (_ball.Rectangle.IntersectsWith(brick.Rectangle))
                {
                    //小球和brick撞上了
                    _ball.ChangeVerDiction();
                    brickNow = brick; //记下来
                    break;
                }
            }
            if(brickNow !=null)
            {
                Bricks.Remove(brickNow);
            }
        }


        /// <summary>
        /// 绘图游戏场景
        /// </summary>
        /// <param name="g"></param>
        /// <param name="size"></param>
        public void Draw(Graphics g,Size size)
        {
            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);

            Graphics _g = Graphics.FromImage(img);
            //定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));
            _g.DrawImage(Properties.Resources.timg_看图王,rec);

            _bar.Draw(_g);
            _ball.Draw(_g);

            foreach (var brick in Bricks)
            {
                brick.Draw(_g);
            }

            //todo

            g.DrawImage(img, new Rectangle(new Point(0, 0), size));

        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>

        public void KeyDown(String key)
        {
            _bar.KeyDown(key);
        }
    }

}
